<?php

class CustomerTypeAction extends CommonAction {

    // 查询数据
    public function index() {
        $this->display();
    }
	
	 public function indexjson() {
	 
		$page = max(1, intval( $_POST['page'] ) );
		$rows = intval( $_POST['rows'] ) ? intval( $_POST['rows'] ) : 20;
		$limitstart = $rows*($page-1);
		
        $CustomerType = M("CustomerType");
        $list = $CustomerType->limit( $limitstart , $rows)->order('id desc')->select();
		$row_tot = $CustomerType->count();

		$i = 0;
		foreach($list as $row ){
			$customertype[$i]['table_id'] = $row['id'];
			$customertype[$i]['table_name'] = $row['name'];
			$customertype[$i]['table_info'] = $row['info'];
			$customertype[$i]['table_status'] = $row['status'];
			$customertype[$i]['table_sort']= $row['sort'];
			$i++;
		}
	
		$customertypeHTML['rows'] = $customertype;
		$customertypeHTML['total'] = $row_tot;
        echo json_encode($customertypeHTML);
        
    }

    // 写入数据
    public function add() {
        $CustomerType = D("CustomerType");
        if ($vo = $CustomerType->create()) {
            $list = $CustomerType->add();
            if ($list !== false) {
                $this->success('数据保存成功！');
            } else {
                $this->error('数据写入错误！');
            }
        } else {
            $this->error($CustomerType->getError() );
        }
    }

    // 更新数据
    public function edit() {
        $CustomerType = D("CustomerType");
        if ($vo = $CustomerType->create()) {
            $list = $CustomerType->save();
            if ($list !== false) {
                $this->success('数据更新成功！');
            } else {
                $this->error("没有更新任何数据!");
            }
        } else {
            $this->error($CustomerType->getError());
        }
    }

    // 删除数据
    public function remove() {
		$id = intval( $_POST['form_id'] );
		if (!empty($id)) {
            $CustomerType = M("CustomerType");
            $result = $CustomerType->delete($id);
            if (false !== $result) {
                $this->success('删除成功！');
            } else {
                $this->error('删除出错！');
            }
        } else {
            $this->error('ID错误！');
        }
    }
}
?>